package dth.bean;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;

import dth.dao.DealerDAO;
import dth.dao.DistributorDAO;
import dth.dao.FaqDAO;
import dth.entity.Dealer;
import dth.entity.Distributor;
import dth.entity.Faq;
import dth.utils.DthUtil;

@ManagedBean(name="distributorBean")
public class DistributorBean {
	private int id=0;
	private String name;
	private java.util.List<Distributor> distributors = new ArrayList<Distributor>();
		
	public java.util.List<Distributor> getDistributors() {
		DistributorDAO dao = new DistributorDAO();
		System.out.println("-------user id:" + LoginBean.id);
		distributors = dao.getDistributorByID(LoginBean.id);
		return distributors;
	}
	public void setDistributors(java.util.List<Distributor> distributors) {
		this.distributors = distributors;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public java.util.List<Distributor> getListDistributor()
	{
		DistributorDAO  distributorDAO=new DistributorDAO();
		return distributorDAO.getListDistributor();
	}
	
	public DistributorBean() {
		System.out.println("---request url:"+DthUtil.getRequestURL());
		if(DthUtil.getRequestURL().contains("distributor.xhtml")){
			System.out.println("--vao day role:"+LoginBean.roleCode);
			if(LoginBean.roleCode!=1)
				DthUtil.navigateTo("welcome.xhtml");
		}
		// TODO Auto-generated constructor stub
		System.out.println("--Distributor bean");
		Object objectAction=DthUtil.getRequest().getParameter("action");
		System.out.println("----------action:"+objectAction);
		if(objectAction!=null)
		{
			if(objectAction.equals("delete"))
			{
				Object objectId=DthUtil.getRequest().getParameter("distributor_id");
				if(objectId!=null)
					id=Integer.parseInt(objectId.toString());
				System.out.println("---id delete:"+id);
			}
			else if(objectAction.equals("update"))
			{
				Object objectId=DthUtil.getRequest().getParameter("distributor_id");
				if(objectId!=null)
					id=Integer.parseInt(objectId.toString());
				System.out.println("---id edit:"+id);
				for(Distributor distributor:getListDistributor())
				{
					if(distributor.getDistributorId()==id)
					{
						this.name=distributor.getDistributorName();
						
					}
				}
				System.out.println("-----name:"+name);
			}
		}
	}
	
	public void insert()
	{
		System.out.println("---id="+id);
		try{
			DistributorDAO distributorDAO=new DistributorDAO();
			Distributor distributor1=new Distributor();
			distributor1.setDistributorId(id);
			distributor1.setDistributorName(name);
			System.out.println(id+"--------");
			
			
			java.util.Date date=new java.util.Date();//lay ngay hien tai
			distributor1.setDateInsert(new Date(date.getTime()));
			int count=0;
			
			if(id==0)
				count=distributorDAO.insert(distributor1);
			else
				count=distributorDAO.update(distributor1);
			
			if(count>0)
				DthUtil.showMessage("Thông Báo", "Insert thành công");
			else
				DthUtil.showMessage("Thông Báo", "Insert thất bại");
		}
		catch(Exception ex){
			DthUtil.showMessage("Thông Báo", "Insert thất bại");
		}
	}
	public void delete()
	{
		DistributorDAO  distributorDAO=new DistributorDAO();
		int count=distributorDAO.delete(id);
		if(count>0)
			DthUtil.showMessage("Thông Báo", "Ä�Ă£ xĂ³a thĂ nh cĂ´ng");
		else
			DthUtil.showMessage("Thông Báo", "Ä�Ă£ xĂ³a tháº¥t báº¡i");
	}
	
}
